package com.xm.servlet;

import com.xm.biz.CartBiz;
import com.xm.biz.impl.CartBizImpl;
import com.xm.po.Cart;
import com.xm.po.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author xie'lin'feng
 * @date 2020-08-21-15:20
 * @description 删除选中的购物车信息
 */
@WebServlet("/deleteIsCheck")
public class deleteCartCheckServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Users users = (Users) session.getAttribute("user");
        CartBiz cartBiz = new CartBizImpl();
        List<Cart> cartList = cartBiz.getAll(users.getUserId());
        //删除选中的购物车信息
        for (Cart cart : cartList) {
            if (cart.getIsCheck()==1){
                cartBiz.delete(cart.getGoodsId(),users.getUserId());
            }
        }
        //获取购物车选中的数量
        List<Cart> cartByIdChecked = cartBiz.getCartByIdChecked(users.getUserId());
        session.setAttribute("sum",cartByIdChecked);
        session.setAttribute("cartList",cartBiz.getAll(users.getUserId()));
        resp.sendRedirect("shopcart.jsp");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
